/*
 * Copyright(C),2024-2024, HELLO.Co.Ltd.
 * Author :
 * date: 2024-06-11 19:14:21
 * File   : task_uart_get.c
 * History:
 */

#include "task_uart_get.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sys_errno.h"
#include "systick.h"

#define ERR_OK (1)
#define ERR_SET_FUNC (0)
#define DelayMs delay_1ms

#define CFG_STR_HEAD_TOKEN "<HELLO_"
#define STR_ARR_SZ (5)
#define STR_SZ (64)
static char g_strArr[STR_ARR_SZ][STR_SZ];
static int g_strArrCnt;
static char g_str[STR_SZ] = "<HELLO_SET_PHASE=30>";

static void beep( void )
{
    
}
/* style :  "="
return: 0 失败 */
static int strToArr(char *str, char *style)
{
    char *token;
    /* 清理字符串 */
    memset(&g_strArr[0][0], 0, sizeof(g_strArr));
    g_strArrCnt = 0;

    /* 获取第一个子字符串 */
    token = strtok(str, style);

    /* 遍历每个子字符串 */
    while (token != NULL) {
        // PRINT_DBG(" %s\n", token);
        if (g_strArrCnt >= STR_ARR_SZ) {
            break;
        }
        strncpy(&g_strArr[g_strArrCnt++][0], token, strlen(token));
        token = strtok(NULL, style);
    }

    return g_strArrCnt;
}

static int setFuncPara(char *pFunc, char *pPara, char *funcName, int (*func)(int pwm))
{
    int ret;
    PRINT_ERR_RET(!pFunc || !pPara || !funcName || !func, ret, "INTPUT PARA NULL\r\n");
    IF_RETURN(strlen(funcName) != strlen(pFunc), ERR_SET_FUNC); /* 添加函数名长度判断 */

    ret = strncmp(pFunc, funcName, strlen(funcName));
    if (!ret) {
        int num = (int)strtol(pPara, NULL, 10);
        // PRINT_INFO("numBuf:%s strlen:%d num:%d\r\n", pPara, strlen(pPara), num);
        beep();

        ret = func(num);
        return ret;
    }
    return ERR_SET_FUNC;
}

static int HELLO_SET_VAC_TARGET(int para)
{
    printf("[%s](%d)\r\n", __func__, __LINE__);
}

static void decArrFunc(void)
{
    int ret = 0;
    PRINT_ERR_RET_VOID(g_strArrCnt < 2, "arr cnt is < 2\r\n");
    char *pFunc = &g_strArr[0][0];
    char *pPara = &g_strArr[1][0];

    /* 去掉参数后面的 >0D */
    for (size_t i = 0; i < strlen(pPara); i++) {
        if (pPara[i] == '>') {
            pPara[i] = 0;
            pPara[i + 1] = 0;
            ret = 1;
        }
    }
    IF_RETURN_VOID(!ret);

    /* HELLO_SET_VAC_TARGET */
    ret = setFuncPara(pFunc, pPara, "HELLO_SET_VAC_TARGET", HELLO_SET_VAC_TARGET);
    PRINT_INFO_RET_VOID(ret == ERR_OK, "HELLO_SET_VAC_TARGET success.\r\n");

    /* 清空buf */
    memset(g_strArr, 0, STR_ARR_SZ * STR_SZ);
}

static int initTaskUartGet(void)
{
    /* 初始化公共资源 */
#if 0
    UartPeriphInit();
#endif

    setvbuf(stdin, NULL, _IONBF, 0);  // 不缓冲stdin
    // setvbuf(stdin, g_str, _IOLBF, STR_SZ);   //
    setvbuf(stdout, NULL, _IONBF, 0);  // 不缓冲stdout
    return ERR_OK;
}

void processTaskUartGet(void *params)
{
    int ret;
    (void)initTaskUartGet();
    PRINT_INFO("processTaskUartGet start\t\t]\r\n");

    while (1) {
        fflush(stdin);
        memset(g_str, 0, STR_SZ);
        /* 输入字符串 */

        char *p = fgets(g_str, sizeof(g_str), stdin);
        printf("You entered: %s\r\n", g_str);

        /* 识别头 */
        /* 找到起始字头 token <HELLO_ */
        char *pFunc = NULL;
        int len = strlen(g_str);
        int lenTag = strlen(CFG_STR_HEAD_TOKEN);
        if (len < lenTag) {
            continue;
        }
        for (size_t i = 0; i < len - lenTag; i++) {
            ret = strncmp(&g_str[i], CFG_STR_HEAD_TOKEN, lenTag);
            if (!ret) {
                pFunc = &g_str[i];
                break;
            }
        }
        if (!pFunc) {
            continue;
        }

        /* 字符数组切割 */
        ret = strToArr(&pFunc[1], "="); /* 去掉< */
        if (!ret) {
            continue;
        }

        /* 解析函数 赋值 */
        decArrFunc();
        DelayMs(10);
    }
}

#if 1

static TaskHandle_t g_uartGetTaskHandle = NULL;
BaseType_t creatTaskUartGet(void)
{
    return xTaskCreate(processTaskUartGet, "TaskUartGet", 1024, (void *)0, configMAX_PRIORITIES - 7, &g_uartGetTaskHandle);
}
#endif